package ruifeng.zcj.dig.base;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.text.TextUtils;

import com.baidu.mapapi.SDKInitializer;
import com.bzh.log.BuildConfig;
import com.bzh.log.MyLog;

import com.lidroid.xutils.HttpUtils;
import com.squareup.leakcanary.LeakCanary;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.MemoryCookieStore;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.OkHttpClient;
import ruifeng.zcj.dig.base.api.ApiHttpClient;
import ruifeng.zcj.dig.base.cache.CacheUtil;
import ruifeng.zcj.mybase.base.App;

/**
 * Created by Administrator on 2016/8/30 0030.
 */
public class MyApp extends App {

    private String userId;

    @Override
    public void onCreate() {
        super.onCreate();
        //LeakCanary检测OOM  oom出的原因就是内存泄漏
        LeakCanary.install(this);



        MyLog.init(BuildConfig.DEBUG);

        init();

        // okhttp();


    }

    private void okhttp() {
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
//        CookieJarImpl cookieJar1 = new CookieJarImpl(new MemoryCookieStore());
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                .addInterceptor(new LoggerInterceptor("TAG"))
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    private void init() {
        // 初始化网络请求
        HttpUtils client = new HttpUtils();
        ApiHttpClient.setHttpClient(client);

        // 缓存
        CacheUtil.setCacheClient(new CacheUtil(getAppContext()));
    }

    /**
     * 用户注销
     */
    public void Logout() {

       /* cleanLoginInfo();
        ApiHttpClient.cleanCookie();
        this.cleanCookie();
        this.login = false;
        this.loginUid = 0;*/

    }

    /****
     * 打包
     **/
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }


    public String getUserId() {
        if (!TextUtils.isEmpty(userId))
            return userId;
        else return null;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}
